//import { _decorator, Button, Component, Node } from 'cc';
const { ccclass, property } = cc._decorator;

@ccclass('SideBar')
export class SideBar extends cc.Component {
     
    private isFromSidebar = false //状态，表示是否从侧边栏进入
    @property({ type: cc.Button })
    public btnSidebar: cc.Button | null = null; //入口有礼按钮
    @property({ type: cc.Node })
    public ndSidebar: cc.Node | null = null; //入口有礼对话框
    @property({ type: cc.Button })
    public btnGotoSidebar: cc.Button | null = null; //去侧边栏按钮
    // @property({ type: cc.Button })
    // public btnGetAward: cc.Button | null = null; //领取奖励按钮
 
 
    start(){
 
        if(!window.tt){
            return;
        }
        tt.onShow((res) => {
            //判断用户是否从侧边栏进来
            this.isFromSidebar = (res.launch_from == 'homepage' && res.location == 'sidebar_card')
            if(this.isFromSidebar){
                //从侧边栏进来，显示‘领取奖励按钮’，隐藏‘去侧边栏按钮’
                //this.btnGetAward.node.active = true
                this.btnGotoSidebar.node.active = true
            }
            else{
                //否则反之
                //this.btnGetAward.node.active = false
                this.btnGotoSidebar.node.active = true
            }
        });
        //判断用户是否支持侧边栏进入功能，不支持就把‘入口有礼’隐藏
        tt.checkScene({
            scene: "sidebar",
            success: (res) => {
                let curtime = new Date().getTime();
                //if(StringUtil.getTimeFormat(curtime,"YY.MM.DD") != PlayerMgr.getLoginDate()){
                    this.btnSidebar.node.active = true;
                //}
            },
            fail: (res) => {
                this.btnSidebar.node.active = false
            }
        });
    }
 
    //以下方法分别绑定对应按钮的事件：
 
    //点击‘入口有礼按钮’，显示‘入口有礼对话框’
    onbtnOpenSideBarDialog(){
        this.ndSidebar.active = true
    }
 
    //关闭‘入口有礼对话框’
    onbtnCloseSideBarDialog(){
        this.ndSidebar.active = false
    }
 
    //‘去侧边栏按钮’逻辑
    onbtnGotoSidebarClick(){
        tt.navigateToScene({
            scene: "sidebar",
            success: (res) => {
                console.log("navigate to scene success！");
                // 跳转成功回调逻辑
            },
            fail: (res) => {
                console.log("navigate to scene fail: ", res);
                console.log('跳转失败，请稍后再试！')
                // 跳转失败回调逻辑
            },
        });
    }
 
    //‘领取奖励按钮’的逻辑
    onbtnGetAwardClick(){
        //获取奖励的逻辑，自己写
        console.log("getAward！");
        let curtime = new Date().getTime();
        //if(StringUtil.getTimeFormat(curtime,"YY.MM.DD") != PlayerMgr.getLoginDate()){
            //PlayerMgr.changeItem({1:1,2:1});
            //UIHelp.ShowGainItemTips({1:1,2:1});   
            //PlayerMgr.updateLoginDate();
            //领取奖励后‘入口有礼’按钮隐藏
            this.btnSidebar.node.active = false;
        //}
    }
}


